/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.server.dac.dao;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import pos.client.dac.dao.CorsoDAO;
import pos.domain.Corso;
import pos.domain.Utente;
import pos.server.dac.ConnectionManager;
import pos.server.dac.exceptions.EccezioneCRUD;

/**
 *
 * @author Alessio
 */
public class CorsoDAOImpl extends RemoteServiceServlet implements CorsoDAO
{
    private String SELECT_SQL = "SELECT * FROM corso WHERE Utente = ? AND nome = ?";
    private String INSERT_SQL = "INSERT INTO corso (Utente, nome, anno, semestre, crediti, docente) VALUES (?,?,?,?,?,?)";
    private String UPDATE_SQL = "UPDATE corso SET Utente = ?, nome = ?, anno = ?, crediti = ?, semestre = ?, docente = ? WHERE Utente = ? AND nome = ?";
    private String DELETE_SQL = "DELETE FROM corso WHERE Utente = ? AND nome = ?";
    
    @Override
    public void leggiDatidaDB(Corso obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(SELECT_SQL);
            pstmt.setString(1, obj.getUtente());
            pstmt.setString(2,obj.getNome());
            ResultSet result = pstmt.executeQuery();
            if(result.next()){
                obj.setAnno(result.getInt("anno"));
                obj.setCrediti(result.getInt("crediti"));
                obj.setSemestre(result.getInt("semestre"));
                obj.setDocente(result.getString("docente"));
            }
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void inserisciDatisuDB(Corso obj, Utente u) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_SQL);
            pstmt.setString(1, u.getLogin());
            pstmt.setString(2, obj.getNome());
            pstmt.setInt(3, obj.getAnno());
            pstmt.setInt(4, obj.getSemestre());
            pstmt.setInt(5, obj.getCrediti());
            pstmt.setString(6, obj.getDocente());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void aggiornaDatisuDB(Corso obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_SQL);
            pstmt.setString(1, obj.getUtente());
            pstmt.setString(2, obj.getNome());
            pstmt.setInt(3, obj.getAnno());
            pstmt.setInt(4, obj.getSemestre());
            pstmt.setInt(5, obj.getCrediti());
            pstmt.setString(6, obj.getDocente());
            pstmt.setString(7, obj.getUtente());
            pstmt.setString(8, obj.getNome());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void cancellaDatidaDB(Corso obj, Utente u) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_SQL);
            pstmt.setString(1, u.getLogin());
            pstmt.setString(2, obj.getNome());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }
}
